package org.csu.jpetstoreagain.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstoreagain.domain.Account;
import org.csu.jpetstoreagain.domain.Profile;
import org.csu.jpetstoreagain.persistence.ProfileMapper;
import org.csu.jpetstoreagain.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    //大鸟页面进入登陆页面
    @GetMapping("/signon")
    public String signon(){
        return "/jsp/account/signon";
    }
    //登陆成功，到达大鸟页面

    @PostMapping("/signin")
    public String signin(@RequestParam("username")String username,
                          @RequestParam("password")String password,
                          Model model,
                          HttpSession session){
        String msg="";
        if(username.isEmpty()||password.isEmpty()){
            msg="用户名或密码不能为空";

        }else{
            //用户名密码不为空
            Account account=accountService.getAccount(username);
            if(account==null){
                //用户名不存在
                msg="用户名不存在";
            }else{
                //用户名存在
                Account account1=accountService.getAccount(username,password);
                if(account1==null){
                    //密码不正确
                    msg="密码不正确";
                }
            }
        }
        if(msg.isEmpty()){
            session.setAttribute("loginAccount",accountService.getAccount(username,password));
            session.setAttribute("loginProfile",accountService.getProfile(username));
            return "/jsp/catalog/main";
        }else{
            model.addAttribute("signOnMsg",msg);
            return "/jsp/account/signon";
        }


    }
    //登陆页面到达注册页面
    @GetMapping("/newAccount")
    public String newAccount(){
        return "/jsp/account/newAccount";
    }
    //提交信息，注册,成功则到达登陆页面，否则原页面爆红
    @PostMapping("/newAccountOK")
    public String newAccountOK(String username, String password, String repeatedPassword, String firstName,
                               String lastName, String email, String phone, String address1, String address2,
                               String city, String state, String zip, String country, String languagePreference,
                               String favouriteCategoryId, String listOption, String bannerOption,
                               HttpSession session) throws ServletException, IOException {
        //单选框如果选择了，值为“on”，没有选择，值为“null”
        int listOption2;
        int bannerOption2;
        if(listOption==null){
            listOption2=0;
        }else{
            listOption2=1;
        }
        if(bannerOption==null){
            bannerOption2=0;
        }else{
            bannerOption2=1;
        }
        String msg="";//保存错误信息
        Account account=new Account();
        //判断是否符合格式
        if(username.isEmpty()||password.isEmpty()||repeatedPassword.isEmpty()||firstName.isEmpty()||
        lastName.isEmpty()||email.isEmpty()||phone.isEmpty()||address1.isEmpty()||address2.isEmpty()||
        city.isEmpty()||state.isEmpty()||zip.isEmpty()||country.isEmpty()){
            msg="填写信息不能为空";
        }else{
            //没有空，判断是否符合格式要求
            if(password.equals(repeatedPassword)){

            }else{
                //两个密码不相等
                msg="密码不一致";
            }
        }
        if(msg=="") {
            session.setAttribute("messageAccount","");
            account.setUserid(username);
            account.setPassword(password);
            account.setFirstname(firstName);
            account.setLastname(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddr2(address2);
            account.setAddr1(address1);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            accountService.insertAccount(account,languagePreference,favouriteCategoryId,listOption2,bannerOption2);
            return "/jsp/account/signon";
        }else{
            session.setAttribute("messageAccount",msg);
            return "/jsp/account/newAccount";
        }





    }

    //点击Account进入账户信息页面
    @GetMapping("/myAccount")
    public String myAccount(){
        return "/jsp/account/editAccountForm";
    }
    //点击signout清除账户信息
    @GetMapping("/main")
    public String signout(HttpSession session){
        session.setAttribute("loginAccount",null);
        return "/jsp/catalog/main";
    }
    @GetMapping("/BirdsMain")
    public String toMain(){
        return "/jsp/catalog/main";
    }
    @PostMapping("/editAccountOK")
    public String editAccount(String password, String repeatedPassword, String firstName,
                              String lastName, String email, String phone, String address1, String address2,
                              String city, String state, String zip, String country, String languagePreference,
                              String favouriteCategoryId, String listOption, String bannerOption,
                              HttpSession session){
        //单选框如果选择了，值为“on”，没有选择，值为“null”
        int listOption2;
        int bannerOption2;
        if(listOption==null){
            listOption2=0;
        }else{
            listOption2=1;
        }
        if(bannerOption==null){
            bannerOption2=0;
        }else{
            bannerOption2=1;
        }
        Account account1= (Account) session.getAttribute("loginAccount");
        String msg="";//保存错误信息
        Account account=new Account();
        Profile profile=new Profile();
        //判断是否符合格式
        if(password.isEmpty()||repeatedPassword.isEmpty()||firstName.isEmpty()||
                lastName.isEmpty()||email.isEmpty()||phone.isEmpty()||address1.isEmpty()||address2.isEmpty()||
                city.isEmpty()||state.isEmpty()||zip.isEmpty()||country.isEmpty()){
            msg="填写信息不能为空";
        }else{
            //没有空，判断是否符合格式要求
            if(password.equals(repeatedPassword)){

            }else{
                //两个密码不相等
                msg="密码不一致";
            }
        }
        if(msg=="") {
            session.setAttribute("messageAccount","");
            account.setUserid(account1.getUserid());
            account.setPassword(password);
            account.setFirstname(firstName);
            account.setLastname(lastName);
            account.setEmail(email);
            account.setPhone(phone);
            account.setAddr2(address2);
            account.setAddr1(address1);
            account.setCity(city);
            account.setState(state);
            account.setZip(zip);
            account.setCountry(country);
            profile.setBanneropt(bannerOption2);
            profile.setMylistopt(listOption2);
            profile.setFavcategory(favouriteCategoryId);
            profile.setUserid(account1.getUserid());
            profile.setLangpref(languagePreference);
            accountService.updateAccount(account,profile);
            session.setAttribute("loginAccount",accountService.getAccount(account1.getUserid()));
            session.setAttribute("loginProfile",profile);
            return "/jsp/account/editAccountForm";
        }else{
            session.setAttribute("messageAccount",msg);
            return "/jsp/account/editAccountForm";
        }



    }
    //注册那里的js交互-判断用户名可用与否
    @PostMapping("/newAccountUser")
    public void newAccountUser(HttpServletResponse resp, HttpServletRequest req) throws IOException {
        //下面判断用户名是否可用
        String username=req.getParameter("username");
        //给js的响应是一个文本
        resp.setContentType("text/plain");//响应默认是同步的，这里要设置成异步的
        PrintWriter out=resp.getWriter();
        Account account=accountService.getAccount(username);
        if(account.getUserid()==null||account.getUserid().equals("")){
            //可用
            out.print("Not Exist");
        }else{
            //不可用
            out.print("Exist");
        }
    }
}
